<!DOCTYPE html>
<html>

	<head>
		<!-- 页面meta -->
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<title>医疗管家</title>
		<meta name="description" content="医疗管家">
		<meta name="keywords" content="医疗管家">
		<meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
		<!-- 引入样式 -->
		<link rel="stylesheet" href="../plugins/elementui/index.css">
		<link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
		<link rel="stylesheet" href="../css/style.css">
		<style type="text/css">
			.active {
				background: #F14F4F;
				color: #000;
			}
		</style>
	</head>

	<body class="hold-transition">
		<div id="app">
			<div class="content-header">
				<h1>系统管理<small>会员管理</small></h1>
				<el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
					<el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
					<el-breadcrumb-item>会员管理</el-breadcrumb-item>
					<el-breadcrumb-item>会员档案管理</el-breadcrumb-item>
				</el-breadcrumb>
			</div>

			<div class="app-container">
				<div class="box">
					<div class="filter-container">
						<el-input placeholder="姓名/手机号码" v-model="pagination.queryString" style="width: 200px;" class="filter-item"></el-input>
						<el-button type="success" @click="findPage()" class="dalfBut">查询</el-button>
						<el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
						<el-button type="info" class="butT" @click="downloadexcel()">下载当页数据</el-button>
					</div>


					<div class="el-table el-table--fit el-table--border el-table--enable-row-hover el-table--enable-row-transition"
					 style="width: 100%;margin-bottom: 20px;">
						<div class="el-table__header-wrapper">
							<table cellspacing="0" cellpadding="0" border="0" class="el-table__header" style="width: 100%;">
								<colgroup>
									<col name="el-table_73_column_304" width="100">
									<col name="el-table_73_column_305">
								</colgroup>
								<thead>
									<tr>
										<th style="background-color:rgb(245,247,250);" class="el-table_73_column_304">
											<div class="cell">性别：</div>
										</th>
										<td class="el-table_73_column_305">
											<span class="cell" v-for="(item,index) in sexList" :key="item">
												<el-tag size="small" :class="{active:index==sexIndex}" @click="sexChoose(index,item)">{{item}}</el-tag>
											</span>
										</td>
									</tr>
									<tr>
										<th style="background-color:rgb(245,247,250);" class="el-table_73_column_304">
											<div class="cell">年龄：</div>
										</th>
										<td class="el-table_73_column_305">
											<span class="cell" v-for="(item,index) in ageList" :key="item">
												<el-tag size="small" :class="{active:index==ageIndex}" @click="ageChoose(index,item)">{{item}}</el-tag>
											</span>
										</td>
									</tr>
									<tr>
										<th style="background-color:rgb(245,247,250);" class="el-table_73_column_304">
											<div class="cell">注册时间：</div>
										</th>
										<td class="el-table_73_column_305">
											<span class="cell" v-for="(item,index) in regList" :key="item">
												<el-tag size="small" :class="{active:index==regIndex}" @click="regChoose(index,item)">{{item}}</el-tag>
											</span>
										</td>
									</tr>
								</thead>
							</table>
						</div>
						<div class="el-table__column-resize-proxy" style="display: none;"></div>
					</div>

					<el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
						<el-table-column type="index" align="center" label="序号"></el-table-column>
						<el-table-column prop="name" label="用户姓名" align="center"></el-table-column>
						<el-table-column prop="sex" label="性别" align="center"></el-table-column>
						<el-table-column prop="birthday" label="出生日期" align="center"></el-table-column>
						<el-table-column prop="idcard" label="身份证" align="center"></el-table-column>
						<el-table-column prop="phonenumber" label="手机号码" align="center"></el-table-column>
						<el-table-column prop="regtime" label="注册时间" align="center"></el-table-column>
						<el-table-column prop="email" label="邮箱地址" align="center"></el-table-column>
						<el-table-column label="操作" align="center">
							<template slot-scope="scope">
								<el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
								<el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
							</template>
						</el-table-column>
					</el-table>
					<div class="pagination-container">
						<el-pagination class="pagiantion" @current-change="handleCurrentChange" :current-page="pagination.currentPage"
						 :page-size="pagination.pageSize" layout="total, prev, pager, next, jumper" :total="pagination.total">
						</el-pagination>
					</div>
					<!-- 新增会员弹层 -->
					<div class="add-form">
						<el-dialog title="新增会员" :visible.sync="dialogFormVisible">
							<!-- 被校验的表单必须有这个属性 ref -->
							<el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
								<el-row>
									<el-col :span="12">
										<el-form-item label="用户姓名" prop="name">
											<el-input v-model="formData.name" />
										</el-form-item>
									</el-col>
									<el-col :span="12">
										<el-form-item label="性别" prop="sex">
											<el-select v-model="formData.sex">
												<el-option label="男" value="男"></el-option>
												<el-option label="女" value="女"></el-option>
											</el-select>
										</el-form-item>
									</el-col>
								</el-row>
								<el-row>
									<el-col :span="12">
										<el-form-item label="出生日期" prop="birthday">
											<el-date-picker v-model="formData.birthday" type="date" placeholder="选择日期">
											</el-date-picker>
										</el-form-item>
									</el-col>
									<el-col :span="12">
										<el-form-item label="身份证" prop="idcard">
											<el-input v-model="formData.idcard" />
										</el-form-item>
									</el-col>
								</el-row>
								<el-row>
									<el-col :span="12">
										<el-form-item label="手机号码" prop="phonenumber">
											<el-input v-model="formData.phonenumber"></el-input>
										</el-form-item>
									</el-col>
									<el-col :span="12">
										<el-form-item label="注册时间" prop="regtime">
											<el-date-picker v-model="formData.regtime" type="date" placeholder="选择日期">
											</el-date-picker>
										</el-form-item>
									</el-col>
								</el-row>
								<el-row>
									<el-col :span="12">
										<el-form-item label="邮箱" prop="email">
											<el-input v-model="formData.email"></el-input>
										</el-form-item>
									</el-col>
									<el-col :span="12">
										<el-form-item label="说明">
											<el-input v-model="formData.remark" type="textarea"></el-input>
										</el-form-item>
									</el-col>
								</el-row>
							</el-form>
							<div slot="footer" class="dialog-footer">
								<el-button @click="dialogFormVisible = false">取消</el-button>
								<el-button type="primary" @click="handleAdd('dataAddForm')">确定</el-button>
							</div>
						</el-dialog>
					</div>

					<!-- 编辑会员弹层 -->
					<div class="add-form">
						<el-dialog title="编辑会员" :visible.sync="dialogFormVisible4Edit">
							<el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
								<el-row>
									<el-col :span="12">
										<el-form-item label="用户姓名" prop="name">
											<el-input v-model="formData.name" />
										</el-form-item>
									</el-col>
									<el-col :span="12">
										<el-form-item label="性别" prop="sex">
											<el-select v-model="formData.sex">
												<el-option label="男" value="男"></el-option>
												<el-option label="女" value="女"></el-option>
											</el-select>
										</el-form-item>
									</el-col>
								</el-row>
								<el-row>
									<el-col :span="12">
										<el-form-item label="出生日期" prop="birthday">
											<el-date-picker v-model="formData.birthday" type="date" placeholder="选择日期">
											</el-date-picker>
										</el-form-item>
									</el-col>
									<el-col :span="12">
										<el-form-item label="身份证" prop="idcard">
											<el-input v-model="formData.idcard" />
										</el-form-item>
									</el-col>
								</el-row>
								<el-row>
									<el-col :span="12">
										<el-form-item label="手机号码" prop="phonenumber">
											<el-input v-model="formData.phonenumber"></el-input>
										</el-form-item>
									</el-col>
									<el-col :span="12">
										<el-form-item label="邮箱" prop="email">
											<el-input v-model="formData.email"></el-input>
										</el-form-item>
									</el-col>
								</el-row>
								<el-row>
									<el-col :span="24">
										<el-form-item label="说明">
											<el-input v-model="formData.remark" type="textarea"></el-input>
										</el-form-item>
									</el-col>
								</el-row>
							</el-form>
							<div slot="footer" class="dialog-footer">
								<el-button @click="dialogFormVisible4Edit = false">取消</el-button>
								<el-button type="primary" @click="handleEdit('dataEditForm')">确定</el-button>
							</div>
						</el-dialog>
					</div>
				</div>
			</div>
		</div>
	</body>
	<!-- 引入组件库 -->
	<script src="../js/vue.js"></script>
	<script src="../plugins/elementui/index.js"></script>
	<script type="text/javascript" src="../js/jquery.min.js"></script>
	<script src="../js/axios-0.18.0.js"></script>
	<script>
		var vue = new Vue({
			el: '#app',
			data: {
				pagination: { //分页相关模型数据
					currentPage: 1, //当前页码
					pageSize: 10, //每页显示的记录数
					total: 0, //总记录数
					querySex:'不限',
					queryAge:'不限',
					queryReg:'不限',
					queryString: null //查询条件
				},
				sexList: ['不限', '男', '女'], //性别的数据
				ageList:['不限', '0-18', '19-29', '30-39', '40-49', '50-59', '60以上'],//年龄数据
				regList:[],//注册时间数据
				sexIndex: -1,
				ageIndex:-1,
				regIndex:-1,
				filterList: [], //筛选的数据
				dataList: [], //当前页要展示的分页列表数据
				formData: {}, //表单数据
				dialogFormVisible: false, //增加表单是否可见
				dialogFormVisible4Edit: false, //编辑表单是否可见
				showFlagConditions: false, //设置是否选中
				rules: { //校验规则
					name: [{
							required: true,
							message: '用户姓名为必填项',
							trigger: 'blur'
						},
						{
							min: 2,
							max: 5,
							message: '长度在 3 到 5 个字符',
							trigger: 'blur'
						}
					],
					sex: [{
						required: true,
						message: '性别为必填项',
						trigger: 'blur'
					}],
					birthday: [{
						required: true,
						message: '出生日期为必填项',
						trigger: 'blur'
					}],
					regTime: [{
						required: true,
						message: '注册时间为必填项',
						trigger: 'blur'
					}],
					phoneNumber: [{
							required: true,
							message: '电话号码为必填项',
							trigger: 'blur'
						},
						{
							//手机号码校验
							validator: function(rule, value, callback) {
								if (/^1[34578]\d{9}$/.test(value) == false) {
									callback(new Error("手机号码不符合规范"));
								} else {
									callback();
								}
							},
							trigger: 'blur'
						}
					],
					idCard: [{
							required: true,
							message: '身份证为必选项',
							trigger: 'blur'
						},
						{
							validator: function(rule, value, callback) {
								if (/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(value) == false) {
									return callback(new Error('证件号码不符合规范'));
								} else {
									callback();
								}
							},
							trigger: 'blur'
						}
					]
				}
			},
			//钩子函数，VUE对象初始化完成后自动执行
			created() {
				this.listPage();
				this.listRegTime();
			},
			methods: {
				// 性别筛选
				sexChoose(index, val) {
					if (index != this.sexIndex) {
						this.sexIndex = index;
						this.pagination.querySex = val;
						this.listPage();
					}else {
						this.sexIndex = -1;
						this.pagination.querySex='不限';
						this.listPage();
					}
				},
				// 年龄筛选
				ageChoose(index, val) {
					if (index != this.ageIndex) {
						this.ageIndex = index;
						this.pagination.queryAge = val;
						this.listPage();
					}else {
						this.ageIndex = -1;
						this.pagination.queryAge='不限';
						this.listPage();
					}
				},
				// 注册时间筛选
				regChoose(index, val) {
					if (index != this.regIndex) {
						this.regIndex = index;
						this.pagination.queryReg = val;
						this.listPage();
					}else {
						this.regIndex = -1;
						this.pagination.queryReg='不限';
						this.listPage();
					}
				},
				
				// 查询注册时间
				listRegTime(){
					axios.get(`http://localhost:9002/member/years`).then(response => {
						this.regList = response.data;
					});
				},
				// 下载当页数据
				downloadexcel(){
					location.href = `http://localhost:9002/member/downloadexcel/${this.pagination.currentPage}/${this.pagination.pageSize}`;
				},
				//查询列表
				listPage() {
					axios.post(`http://localhost:9002/member/list`,this.pagination).then(response => {
						console.log(response.data);
						this.pagination.total = response.data.total;
						this.dataList = response.data.rows;
					});
				},
				//编辑用户
				handleEdit(dataEditForm) {
					this.$refs[dataEditForm].validate(valid => {
						if (valid) {
							axios.put(`http://localhost:9002/member/update`, this.formData).then(response => {
								this.myMessage(response);
							});
						} else {
							this.$message.warning("验证未通过");
							return false;
						}
					});
				},
				//添加用户
				handleAdd(dataAddForm) {
					this.$refs[dataAddForm].validate(valid => {
						if (valid) {
							axios.post(`http://localhost:9002/member/add`, this.formData).then(response => {
								this.myMessage(response);
							});
						} else {
							this.$message.warning("验证未通过");
						}
					});

				},
				//分页查询
				findPage() {
					this.listPage();
				},
				//消息处理
				myMessage(response) {
					if (response.data.flag) {
						this.$message.success(response.data.message);
						this.listPage();
						this.resetForm();
					} else {
						this.$message.error(response.data.message);
					}
				},
				//重置表单
				resetForm() {
					this.dialogFormVisible4Edit = false;
					this.dialogFormVisible = false;
					//清空表单
					this.formData = {};
				},
				//弹出添加窗口
				handleCreate() {
					this.formData = {};
					this.dialogFormVisible = true;
				},
				//弹出编辑窗口
				handleUpdate(row) {
					axios.get(`http://localhost:9002/member/${row.id}`).then(response => {
						if (response.data.flag) {
							this.dialogFormVisible4Edit = true;
							this.formData = response.data.data;
						} else {
							this.$message.warning(response.data.message);
						}
					});
				},
				//切换页码显示分页信息
				handleCurrentChange(currentPage) {
					this.pagination.currentPage = currentPage;
					this.listPage();
				},
				// 删除用户项
				handleDelete(row) {
					this.$confirm('此操作将永久删除该会员, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					}).then(() => {
						axios.delete(`http://localhost:9002/member/${row.id}`).then(response => {
							this.myMessage(response);
						});

					}).catch(() => {
						this.$message({
							type: 'info',
							message: '已取消删除'
						});
					});
				}
			}
		})
	</script>

</html>
